package com.kai.mqtt.task;

import com.kai.mqtt.statistics.IStatisticsService;
import com.kai.mqtt.data.response.LatestDataResponse;
import com.kai.mqtt.message.service.ISubscribeMessageService;
import com.kai.mqtt.session.service.ISessionService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author weijk
 * @since 2024/7/1
 */
@Component
public class StatisticsTask {

    @Resource
    private ISubscribeMessageService subscribeMessageService;

    @Resource
    private ISessionService sessionService;

    @Resource
    private IStatisticsService statisticsCacheService;

    @Scheduled(cron = "0/1 * * * * ?")
    public void statistics() {
        statisticsCacheService.put(new LatestDataResponse()
                .setMsgInflow(statisticsCacheService.getMsgInflow())
                .setMsgOutflow(statisticsCacheService.getMsgOutflow())
                .setConnect(sessionService.getConnect())
                .setOnlineConnect(sessionService.getOnlineConnect())
                .setTopics(subscribeMessageService.getTopics())
                .setSubscriptions(subscribeMessageService.getSubscribes())
                .setDateTime(LocalDateTime.now().withNano(0)));
    }
}
